﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig.Controller
{
    public class PaperTypeController
    {
        #region //private fields

        private PaperTypeBLL            _bll = null;
        private CheckedListBoxControl   _listBox = null;
        private GridControl             _grid = null;
        private GridView                _view = null;
        private ComboBoxEdit            _comboBox = null;
        private GroupControl            _groupControl = null;
        private CheckEdit               _checkEdit = null;

        #endregion


        #region //Public Properties

        internal CheckedListBoxControl ListBox
        {
            set
            {
                _listBox = value;
            }
            get
            {
                return _listBox;
            }
        }

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// Get Printers when the form loadding
        /// </summary>
        public void GetDate()
        {
            _bll = new PaperTypeBLL();
            _grid.DataSource = _bll.GetPaperTypeList();
        }

        /// <summary>
        /// 添加纸型
        /// </summary>
        /// <param name="obj"></param>
        public void Add(paperType obj)
        {
            BordWidthBLL bordWidthBLL = new BordWidthBLL();
            obj.BoardWidth = bordWidthBLL.GetBordWidthObj(Convert.ToDouble(_comboBox.Text));
            for (int i = 0; i < _view.RowCount; i++)
            {
                BoardWidth bordWidth = new BoardWidth();
                BordWidthEntity bordWidthEntity = (BordWidthEntity)_view.GetRow(i);
                if (bordWidthEntity.IsChecked)
                {
                    bordWidth = bordWidthBLL.GetBordWidthObj(bordWidthEntity.Width);
                    obj.BoardWidth1.Add(bordWidth);
                }
            }
            _bll.Add(obj);
        }

        /// <summary>
        ///更新纸型
        /// </summary>
        /// <param name="obj"></param>
        public void Update(paperType obj)
        {
            BordWidthBLL bordWidthBLL = new BordWidthBLL();
            _bll = new PaperTypeBLL();
            obj.BoardWidth = bordWidthBLL.GetBordWidthObj(Convert.ToDouble(_comboBox.Text));
            obj.BoardWidth1.Clear();
            for (int i = 0; i < _view.RowCount; i++)
            {
                BoardWidth bordWidth = new BoardWidth();
                BordWidthEntity bordWidthEntity = (BordWidthEntity)_view.GetRow(i);
                if (bordWidthEntity.IsChecked)
                {
                    bordWidth = bordWidthBLL.GetBordWidthObj(bordWidthEntity.Width);
                    obj.BoardWidth1.Add(bordWidth);
                }
            }
            _bll.Update();
        }
        /// <summary>
        /// 停用纸型
        /// </summary>
        /// <param name="obj"></param>
        public void Disable(paperType obj)
        {
            //_bll = new PaperFaceBLL();
            //_bll.disable(obj);
        }
        /// <summary>
        /// 启用纸型
        /// </summary>
        /// <param name="obj"></param>
        public void Useable(paperType obj)
        {
            //_bll = new PaperFaceBLL();
            //_bll.Useable(obj);
        }
        /// <summary>
        /// 删除纸型
        /// </summary>
        /// <param name="obj"></param>
        public void Delete(paperType obj)
        {
            _bll = new PaperTypeBLL();
            _bll.Delete(obj);
        }
        /// <summary>
        /// 得到纸型对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public paperType GetPaperTypeObj(string name)
        {
            _bll = new PaperTypeBLL();
            return _bll.GetPaperTypeObj(name);
        }
        /// <summary>
        /// 得到纸型对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public paperType GetPaperTypeObj(int id)
        {
            _bll = new PaperTypeBLL();
            return _bll.GetPaperTypeObj(id);
        }
        /// <summary>
        /// 添加纸型时绑定该纸型对应的纸型宽度
        /// </summary>
        public void LinkBordWidth()
        {
            List<BordWidthEntity> bordWidthEntityList = new List<BordWidthEntity>();
            BordWidthBLL bordWidthBLL = new BordWidthBLL();
            foreach (var item in bordWidthBLL.GetBordWidthList())
	        {
		        BordWidthEntity bordWidthEntity = new BordWidthEntity();
                bordWidthEntity.WidthId = item.BoardWidthID;
                bordWidthEntity.Width = item.Width;
                bordWidthEntityList.Add(bordWidthEntity); 
	        }

            _grid.DataSource = bordWidthEntityList;
                
        }
        /// <summary>
        /// 焦点行改变时绑定该纸型对应的纸型宽度
        /// </summary>
        /// <param name="id"></param>
        public void LinkBordWidthFocuseChanged(int id)
        {
            List<BordWidthEntity> bordWidthEntityList = new List<BordWidthEntity>();
            paperType type = _bll.GetPaperTypeObj(id);
            BordWidthBLL bordWidthBLL = new BordWidthBLL();
            foreach (var item in bordWidthBLL.GetBordWidthList())
            {
                BordWidthEntity bordWidthEntity = new BordWidthEntity();
                bordWidthEntity.WidthId = item.BoardWidthID;
                bordWidthEntity.Width = item.Width;
                if (!type.BoardWidth1.IsLoaded)
                {
                    type.BoardWidth1.Load();
                }
                foreach (var bordWidth in type.BoardWidth1)
                {
                    if (item.BoardWidthID == bordWidth.BoardWidthID)
                    {
                        bordWidthEntity.IsChecked = true;
                    }
                }
                bordWidthEntityList.Add(bordWidthEntity); 
            }
            _grid.DataSource = bordWidthEntityList;
 
        }
        public void LinkDefaultBordWidth()
        {

            //BordWidthBLL bordWidthBLL = new BordWidthBLL();
            //foreach (var item in bordWidthBLL.GetBordWidthList())
            //{
            //    _comboBox.Properties.Items.Add(item.Width);
            //}
            _comboBox.Properties.Items.Clear();
            for (int i = 0; i < _view.RowCount; i++)
            {
                BordWidthEntity boardWidth = (BordWidthEntity)_view.GetRow(i);
                if (boardWidth.IsChecked)
                {
                    _comboBox.Properties.Items.Add(boardWidth.Width);
                }

            }
        }

        public string GetDefaultBordWidth(paperType obj)
        {
            paperType type = _bll.GetPaperTypeObj(obj.PaperTypeID);
            return type.BoardWidth.Width.ToString();
        }


        #endregion
    }
   
}
